package com.fy.service.service.impl;

import java.util.Objects;

import com.fy.common.dto.*;
import com.fy.dao.mapper.SkImUserInfoMapper;
import com.fy.dao.mapper.SkImUserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fy.framework.common.model.R;
import com.fy.framework.component.config.LocaleMessage;
import com.fy.common.entity.SkImUser;
import com.fy.common.entity.SkImUserInfo;
import com.fy.service.service.SkImUserInfoService;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;


/**
 * @author zhshuo
 * @date 2021/12/01 11:22
 **/
@Slf4j
@Service
@AllArgsConstructor
public class SkImUserInfoServiceImpl extends ServiceImpl<SkImUserInfoMapper, SkImUserInfo>
    implements SkImUserInfoService {

    LocaleMessage localeMessage;

    SkImUserMapper skImUserMapper;

    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public R<?> infoComplete(UserInfoCompleteDto infoCompleteDto) {
        if (StringUtils.isNotBlank(infoCompleteDto.getEmail())) {
            SkImUserInfo skImUserInfo = selectOne(new EntityWrapper<SkImUserInfo>()
                .eq("email", infoCompleteDto.getEmail()).ne("sk_im_user_id", infoCompleteDto.getSkImUserId()));
            if (Objects.nonNull(skImUserInfo)) {
                return R.failedAlter(localeMessage.getMessage("im.user.emailExisted"));
            }
        }

        if (StringUtils.isNotBlank(infoCompleteDto.getPhone())) {
            SkImUserInfo skImUserInfo = selectOne(new EntityWrapper<SkImUserInfo>()
                .eq("phone", infoCompleteDto.getEmail()).ne("sk_im_user_id", infoCompleteDto.getSkImUserId()));
            if (Objects.nonNull(skImUserInfo)) {
                return R.failedAlter(localeMessage.getMessage("im.user.phoneExisted"));
            }
        }

        SkImUserInfo imUserInfo = selectOne(new EntityWrapper<SkImUserInfo>().eq("sk_im_user_id", infoCompleteDto.getSkImUserId()));

        SkImUserInfo updateUserInfo = new SkImUserInfo().setSkImUserId(infoCompleteDto.getSkImUserId())
                .setUsername(infoCompleteDto.getUsername())
                .setGender(infoCompleteDto.getGender()).setBirthday(infoCompleteDto.getBirthday())
                .setLocation(infoCompleteDto.getLocation()).setSignature(infoCompleteDto.getSignature())
                .setPhone(infoCompleteDto.getPhone()).setEmail(infoCompleteDto.getEmail())
                .setAvatar(infoCompleteDto.getAvatar());
        boolean result = update(updateUserInfo,
            new EntityWrapper<SkImUserInfo>().eq("sk_im_user_id", infoCompleteDto.getSkImUserId()));
        return result ? R.successAlter(localeMessage.getMessage("im.common.refreshSuccess"))
            : R.failedAlter(localeMessage.getMessage("im.common.failedful"));
    }

    @Override
    public R<?> passwordModify(UserPwdModifyDto passwordModifyDto) {
        String email = passwordModifyDto.getEmail();
        if (StringUtils.isBlank(email)) {
            return R.failedAlter(localeMessage.getMessage("im.user.emailIsNull"));
        }

        SkImUserInfo skImUserInfo =
            selectOne(new EntityWrapper<SkImUserInfo>().eq("sk_im_user_id", passwordModifyDto.getSkImUserId()));
        if (!email.equals(skImUserInfo.getEmail())) {
            return R.failedAlter(localeMessage.getMessage("im.user.emailNotMatch"));
        }

        Integer update = skImUserMapper.updateById(new SkImUser().setId(passwordModifyDto.getSkImUserId())
            .setPassword(bCryptPasswordEncoder.encode(passwordModifyDto.getPassword())));
        return update > 0 ? R.successAlter(localeMessage.getMessage("im.common.refreshSuccess"))
            : R.failedAlter(localeMessage.getMessage("im.common.failedful"));
    }

    @Override
    public R<?> secrecySetting(UserSecrecySettingDto secrecySettingDto) {
        int secrecySetting = baseMapper.secrecySetting(secrecySettingDto);
        return secrecySetting > 0 ? R.successAlter(localeMessage.getMessage("im.common.refreshSuccess"))
            : R.failedAlter(localeMessage.getMessage("im.common.failedful"));
    }

    @Override
    public R<?> strangerDetail(UserDetailQueryDto detailQueryDto) {
        SkImUserInfo userInfo = selectOne(
            new EntityWrapper<SkImUserInfo>().setSqlSelect("sk_im_user_id", "username", "location", "phone", "avatar")
                .eq("sk_im_user_id", detailQueryDto.getTargetUserId()));
        return R.success(userInfo);
    }
}
